import GameWorld
import miscUtil
import service
import world

class GPS(service.Service):
    __guid__ = 'svc.gps'

    def __init__(self, *args, **kwargs):
        self.worldSpaceSvc = None
        self.worldSpacePortalDistances = {}
        service.Service.__init__(self, *args, **kwargs)



    def Run(self, *etc):
        self.worldSpaceSvc = sm.GetService(('worldSpace' + boot.role.title()))
        self.gameWorldService = sm.GetService(('gameWorld' + boot.role.title()))
        self.GPS = self.gameWorldService.GetGameWorldManager().gps



    def LoadPathCostsForWorldSpace(self, worldSpaceID):
        return 
        if (worldSpaceID not in self.worldSpacePortalDistances):
            filePath = (((const.world.PATHING_FOLDER + 'worldSpace_') + str(worldSpaceID)) + '/')
            filePath += const.world.PATHING_DATA_FILE
            pathData = world.BaseWorldSpaceService.LoadPathingDataFile(filePath)
            if (pathData is not None):
                self.worldSpacePortalDistances[worldSpaceID] = pathData['portalCosts']



    def UnloadPathCostsForWorldSpace(self, worldSpaceID):
        if (worldSpaceID in self.worldSpacePortalDistances):
            del self.worldSpacePortalDistances[worldSpaceID]



    def AddPortal(self, instanceID, portalID, toInstanceID, toPortalID, pos, aabb, transform):
        worldSpaceTypeID = self.worldSpaceSvc.GetWorldSpaceTypeIDFromWorldSpaceID(instanceID)
        if (worldSpaceTypeID in self.worldSpacePortalDistances):
            worldDistances = self.worldSpacePortalDistances[worldSpaceTypeID]
            if (portalID in worldDistances):
                portalDistances = worldDistances[portalID]
            else:
                portalDistances = {}
        else:
            portalDistances = {}
        self.GPS.AddPortal(long(instanceID), portalID, long(toInstanceID), toPortalID, pos, aabb, transform, portalDistances)



    def GetPortalsNeededToPath(self, entity, toInstanceID, toPosition):
        return self.GPS.GetPathAcrossInstances(entity.movement, entity.GetWorldSpaceInstanceID(), entity.GetPosition(), toInstanceID, toPosition)



    def ConvertCoordinate(self, instanceID, pos, toInstanceID):
        return self.GPS.ConvertCoordinateToInstance(instanceID, pos, toInstanceID)



    def ConvertCoordinateToConnectedInstances(self, instanceID, pos, maxDist = -1.0):
        return self.GPS.ConvertCoordinateToConnectedInstances(instanceID, pos, maxDist)



    def GetDistancesToConnectedInstances(self, instanceID, pos, maxDist = -1.0):
        return self.GPS.GetDistanceToConnectedInstances(instanceID, pos, maxDist)




